package com.wenli.graduate2022.wenliGreenShop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.graduate2022.wenliGreenShop.entity.*;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户相关的controller
 *
 * @author HuangHai
 * @date 2021/9/26 21:01
 */
@RestController    //声明这是一个restController类，等于@controller与@responseBody的合体，前端控制器就不会走视图解析器了
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "userLogin")
    public User frontLogin(HttpServletRequest request, @RequestBody User user) {//post传值方式，最好用requestBody接收，如果接收的参数太多太散就难以识别，因为前端传过来的的是json对象
        User returnUser = userService.login(user.getUserName(), user.getUserPassword());
        System.out.println(returnUser);
        if (user != null) {
            request.getSession().setAttribute("userLoginStatus", returnUser);
            return returnUser;
        }
        return null;
    }

    @GetMapping("/user")
    public List<User> findAll() {
        System.out.println("查询所有方法执行");
        return userService.findAll();
    }

    @GetMapping("/user/{page}/{size}")
    public IPage<User> findByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<User> userPage = new Page<User>(page, size);
        IPage<User> userIPage = userService.findByPage(userPage);
        System.out.println("打印用户页面：" + userPage.toString());
        System.out.println("分页查询所有用户：" + userIPage.toString());
        return userIPage;
    }

    /**
     * 模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("userByName/{name}/{page}/{size}")
    public IPage<User> findByName(@PathVariable("page") Integer page,@PathVariable("size") Integer size,@PathVariable("name") String name) {
        System.out.println("模糊查询方法执行");
        Page<User> productPage = new Page<User>(page, size);
        return userService.findByName(productPage,name);
    }

    @GetMapping("/user/{id}")
    public User findById(@PathVariable("id") Integer id) {
        System.out.println("查询具体一个方法执行");
        return userService.findById(id);
    }

    @PostMapping("/user")
    public int insert(@RequestBody User user) {
        System.out.println("插入方法执行");
        return userService.insert(user);
    }

    @PutMapping("/user")
    public int update(@RequestBody User user) {
        System.out.println("更新方法执行");
        return userService.update(user);
    }

    @PutMapping("/user/initPassword/{id}")
    public int initPassword(@PathVariable("id") Integer id) {     //将用户密码初始化为123456
        System.out.println("初始化用户密码为123456");
        return userService.initPassword(id);
    }

    @DeleteMapping("user/{id}")
    public int delete(@PathVariable("id") Integer id) {
        System.out.println("删除方法执行");
        return userService.delete(id);
    }

    @PutMapping("user/{id}/{password}")
    public int updatePassword(@RequestBody User user, @PathVariable("id") Integer id, @PathVariable("password") String password){
        return userService.updatePassword(user,id,password);
    }
}
